package com.kk.xhr.admin.controller;

import com.kk.xhr.admin.biz.ISalaryBizService;
import com.kk.xhr.admin.controller.request.SalaryAddRequest;
import com.kk.xhr.admin.controller.request.SalaryModifyRequest;
import com.kk.xhr.admin.controller.request.SalaryQueryRequest;
import com.kk.xhr.admin.controller.vo.SalaryVO;
import com.kk.xhr.common.pojo.BinaryEnum;
import com.kk.xhr.web.util.ShiroUtil;
import com.kk.xhr.web.vo.ApiResult;
import com.kk.xhr.web.vo.PageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.Executor;

/**
 * 薪资管理控制器
 *
 * @author luokexiong
 * @version 1.0 2021/2/19
 * @since 1.0.0
 */
@RestController
@RequiredArgsConstructor
@Api(value = "薪资管理", tags = "薪资管理")
@RequestMapping("/salary")
public class SalaryController {
    private final Executor adminControllerBizExecutor;
    private final ISalaryBizService salaryBizService;

    @ApiOperation(value = "分页查询薪资")
    @RequiresRoles(value = {"admin", "salary"}, logical = Logical.OR)
    @GetMapping
    public CompletionStage<ApiResult<PageVO<SalaryVO>>> queryPage(@Valid SalaryQueryRequest request) {
        return CompletableFuture.supplyAsync(() -> ApiResult.success(salaryBizService.queryPage(request)), adminControllerBizExecutor);
    }

    @ApiOperation(value = "查询我的薪资")
    @GetMapping("/mine")
    public CompletionStage<ApiResult<SalaryVO>> queryMine() {
        Long userId = ShiroUtil.getAuthUserId();
        return CompletableFuture.supplyAsync(() ->
            ApiResult.success(salaryBizService.queryMine(userId)), adminControllerBizExecutor);
    }

    @ApiOperation(value = "查询我的历史薪资")
    @GetMapping("/log")
    public CompletionStage<ApiResult<List<SalaryVO>>> queryLog() {
        Long userId = ShiroUtil.getAuthUserId();
        return CompletableFuture.supplyAsync(() ->
            ApiResult.success(salaryBizService.queryLog(userId)), adminControllerBizExecutor);
    }

    @ApiOperation(value = "查询某个用户的薪资")
    @GetMapping("/report/{userId}")
    public CompletionStage<ApiResult<SalaryVO>> queryReport(@PathVariable("userId") Long userId) {
        return CompletableFuture.supplyAsync(() ->
            ApiResult.success(salaryBizService.query(userId)), adminControllerBizExecutor);
    }

    @ApiOperation(value = "添加薪资")
    @RequiresRoles(value = {"admin", "salary"}, logical = Logical.OR)
    @PostMapping
    public CompletionStage<ApiResult<Void>> add(@RequestBody @Valid SalaryAddRequest request) {
        return CompletableFuture.supplyAsync(() -> {
            salaryBizService.add(request);
            return ApiResult.success();
        }, adminControllerBizExecutor);
    }

    @ApiOperation(value = "修改薪资")
    @RequiresRoles(value = {"admin", "salary"}, logical = Logical.OR)
    @PutMapping("/{id:.+}")
    public CompletionStage<ApiResult<Void>> modify(@PathVariable("id") Long id, @RequestBody @Valid SalaryModifyRequest request) {
        return CompletableFuture.supplyAsync(() -> {
            salaryBizService.modify(id, request);
            return ApiResult.success();
        }, adminControllerBizExecutor);
    }

    @ApiOperation(value = "修改薪资状态")
    @RequiresRoles(value = {"admin", "salary"}, logical = Logical.OR)
    @PutMapping("/status/{id:.+}")
    public CompletionStage<ApiResult<Void>> modifyStatus(@PathVariable("id") Long id, BinaryEnum status) {
        return CompletableFuture.supplyAsync(() -> {
            salaryBizService.modifyStatus(id, status);
            return ApiResult.success();
        }, adminControllerBizExecutor);
    }

    @ApiOperation(value = "结算工资")
    @RequiresRoles(value = {"admin", "salary"}, logical = Logical.OR)
    @PostMapping("/calculate")
    public CompletionStage<ApiResult<Void>> calculateSalary() {
        return CompletableFuture.supplyAsync(() -> {
            salaryBizService.calculateSalary();
            return ApiResult.success();
        }, adminControllerBizExecutor);
    }
}
